//
// Created by jinbang.li on 2023/7/23.
//

#include <thread>
#include "IOpAudioPlay.h"
#include "../Log/OpLog.h"

using namespace std;
void sleep_audio(int millseconds){
    chrono::milliseconds  du(millseconds);
    this_thread::sleep_for(du);
}
OpData IOpAudioPlay::GetData() {
    OpData data;
    while (!isExit){
        framesMutex.lock();
        if (!frames.empty()){
            data = frames.front();
            frames.pop_front();
            framesMutex.unlock();
            return data;
        }
        framesMutex.unlock();
        sleep_audio(1);
    }
    return data;
}
void IOpAudioPlay::update(OpData data) {
    //压入缓存队列
    OPLOGD("IOpAudioPlay::update %d",data.size);
    if (data.size <= 0 || !data.data)return;
    while (!isExit){
        framesMutex.lock();
        if (frames.size() > maxFrame){
            framesMutex.unlock();
            sleep_audio(1);
            continue;
        }
        frames.push_back(data);
        framesMutex.unlock();
        break;
    }
}